import 'dart:typed_data';

///模仿 drift的写法
class ColumnBuilder<T extends Object>{

  ///
  ColumnBuilder(){
    _column = Column<T>._();
  }
  Column<T>? _column;

  ///字段名
  ColumnBuilder<T> named(String name) {
    _column?._name = name;
    return this;
  }

  ///可为空
  ColumnBuilder<T> nullable() {
    _column?.nullable = true;
    return this;
  }

  ///字段的默认值
  ColumnBuilder<T> withDefault(T defaultValue) {
    _column?.defaultValue = defaultValue;
    return this;
  }

  ///字段唯一
  ColumnBuilder<T> unique() {
    _column?.unique = true;
    return this;
  }
  ///
  Column<T> call() {
    return _column!;
  }
}

class Column<T extends Object> {

  Column._();
  
  String? _name;
  T? defaultValue;
  bool nullable = false;
  bool unique = false;

  String get name => _name??'';

  String get type {
    if (T == int) {
      return 'INTEGER';
    } else if (T == BigInt) {
      return 'INTEGER';
    } else if (T == bool) {
      return 'INTEGER';
    } else if (T == String) {
      return 'TEXT';
    } else if (T == DateTime) {
      return 'INTEGER';
    } else if (T == Uint8List) {
      return 'BLOB';
    } else if (T == double) {
      return 'REAL';
    } else {
      return 'ANY';
    }
  }
}

/// A column that stores int values.
typedef IntColumn = Column<int>;

/// A column that stores BigInt values.
///
/// In a database, this is the exact same thing as an [IntColumn]. However,
/// unlike an [IntColumn], the resulting value is mapped to a [BigInt] in Dart.
/// As sqlite3 only supports 64-big integers (the same range that an [int] in
/// Dart can represent), there is no need to use an [Int64Column] for native
/// apps. For Dart compiled to JavaScript though, it may be beneficial to use
/// [BigInt]s to avoid loosing precision for values larger than 2⁵².
typedef Int64Column = Column<BigInt>;

/// A column that stores boolean values. Booleans will be stored as an integer
/// that can either be 0 (false) or 1 (true).
typedef BoolColumn = Column<bool>;

/// A column that stores text.
typedef TextColumn = Column<String>;

/// A column that stores a [DateTime]. Times will be stored as unix timestamp
/// and will thus have a second accuracy.
typedef DateTimeColumn = Column<DateTime>;

/// A column that stores arbitrary blobs of data as a [Uint8List].
typedef BlobColumn = Column<Uint8List>;

/// A column that stores floating point numeric values.
typedef RealColumn = Column<double>;